<?php

namespace App\Models\Common;

use Illuminate\Support\Facades\View;

/**
 * 主题处理类
 * @package App\Models\Common
 */
class ThemeAssets
{
    private static $css;
    private static $js;
    private static $_instance;

    public static function init()
    {
        View::share("themeAssetsCss", self::getCss());
        View::share("themeAssetsJs", self::getJs());
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 添加css样式
     * @param string $cssPath css样式路径
     * @param int $weight 权重，数字越大越靠后
     */
    public static function addCss($cssPath = '', $weight = 0)
    {
        if (!$cssPath) {
            return false;
        }
        $currentCss = '<link rel="stylesheet" href="' . $cssPath . '">';
        $css = self::$css;
        $css[$weight][] = $currentCss;
        self::$css = $css;
    }

    /**
     * 获取css样式的html
     * @return string
     */
    public static function getCss()
    {
        //处理为完全正序
        $cssHtml = '';
        if (self::$css) {
            foreach (self::$css as $css) {
                foreach ($css as $row) {
                    $cssHtml .= $row;
                }
            }
        }

        return $cssHtml;
    }

    /**
     * 添加js样式
     * @param string $jsPath js样式路径
     * @param int $weight 权重，数字越大越靠后
     */
    public static function addJs($jsPath = '', $weight = 0)
    {
        if (!$jsPath) {
            return false;
        }

        $currentJs = '<script src="' . $jsPath . '"></script>';
        $js = self::$js;
        $js[$weight][] = $currentJs;
        self::$js = $js;
    }

    /**
     * 获取js样式的html
     * @return string
     */
    public static function getJs()
    {
        //处理为完全正序
        $jsHtml = '';

        if (self::$js) {
            foreach (self::$js as $js) {
                foreach ($js as $row) {
                    $jsHtml .= $row;
                }
            }
        }
        return $jsHtml;
    }

    /**
     * 输出最终js和css静态资源
     */
    public static function output()
    {
        View::share("themeAssetsCss", self::getCss());
        View::share("themeAssetsJs", self::getJs());
    }

    /**
     * ===================================
     * 组件功能封装
     *
     * 可以调用次方式直接加载指定组件,则不需要再一个一个js或者css文件的引入
     * ===================================
     */
    public static function addComponent($components)
    {
        //字符串处理
        if (is_string($components)) {
            self::addSingleComponent($components);
        }

        //数组处理
        if (is_array($components)) {
            foreach ($components as $item) {
                self::addSingleComponent($item);
            }
        }
    }

    /**
     * 加载单个组件
     * @param $name 组件名字
     */
    private static function addSingleComponent($name = '')
    {
        switch ($name) {
            case 'vue'://vue
                self::addJs("/assets/component/vue/index.js");
                break;
            case 'element'://element
                self::addCss("/assets/component/element/index.css");
                self::addJs("/assets/component/element/index.js");
                break;
            default;
        }
    }
}